<?php


namespace app\admin\model;
use think\facade\Cache;

class Role extends Base
{
    public $routeCacheName = 'adminRoleCache';

    /**
     * Desc: 获取状态码缓存公共方法
     * User: 苏鹏
     * Date: 2023/9/7
     * Time: 16:18
     * @return void
     */
    public function getAdminModelCache(string $type = 'get') : array
    {
        switch ($type){
            case 'get' :
                $cache_res = Cache::get($this->routeCacheName);
                if($cache_res == false){
                    //生成缓存
                    $result = $this->getRoleListToCache();
                    return $result;
                }
                return $cache_res;
            case 'set' :
                $result = $this->getRoleListToCache();
                return $result;
            case 'del' :
                Cache::delete($this->routeCacheName);
                break;
            case 'get_all' :
                Cache::get('adminRoleCacheAll');
                break;
        }
    }

    /**
     * Desc: 获取状态码缓存公共方法 辅助类
     * User: 苏鹏
     * Date: 2023/9/20
     * Time: 10:12
     */
    public function getRoleListToCache():array
    {
        $result = $this->where('status', 1)
            ->field('id')
            ->select()
            ->toArray();
        //查询角色拥有的权限
        $data = [];
        foreach($result as $k=>$v){
            $route_ids_res =(new RoleReRoute())->where('role_id', $v['id'])->column('route_id');
            if(empty($route_ids_res)){
                $data[$v['id']] = [];
            }else{
                $route_res = (new Route())->where('id', 'in', $route_ids_res)->field('id,title,route,rule,method,pid,vue_route')->select()->toArray();
                $data[$v['id']] = $route_res ?: [];
            }
        }
        Cache::set($this->routeCacheName, $data);
        return $data;
    }

    public function getRoleReRouteTreeLists(int $user_id):array
    {
        if(empty($user_id)){
            return [];
        }
        //用户拥有的角色
        $role_re_user_ids = (new RoleReUser())->where('user_id', $user_id)->column('role_id');

        if(empty($role_re_user_ids))
        {
            return [];
        }
        //角色权限缓存
        $role_cache = (new \app\admin\model\Role())->getAdminModelCache('set');
        $route_auth = [];
        foreach($role_re_user_ids as $k=>$v)
        {
            if(!empty($role_cache[$v])){
                $route_auth = array_merge($route_auth, $role_cache[$v]);
            }
        }

        $sort = array_column($route_auth, 'pid');
        array_multisort($sort, SORT_ASC, $route_auth);

        if(!empty($route_auth)){
            $items = [];
            foreach($route_auth as $value){
                $items[$value['id']] = $value;
            }
            //第二部 遍历数据 生成树状结构
            $tree = [];
            foreach($items as $key => $item){
                if(isset($items[$item['pid']])){
                    $items[$item['pid']]['children'][] = &$items[$key];
                }else{
                    $tree[] = &$items[$key];
                }
            }
        }

        return $tree;
    }

}